﻿using FirstFloor.ModernUI.Presentation;
using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;

namespace EasyInvoice.WPF.ViewModel
{
    public class UserListNewViewModel : NotifyPropertyChanged
    {

        #region User List

        private ObservableCollection<UserViewModel> _users = new ObservableCollection<UserViewModel>();

        public ObservableCollection<UserViewModel> Users
        {

            get
            {
                
                this._users = this.GetUsers();
                return this._users;
            }
            set
            {
                this._users = value;
                this.OnPropertyChanged("Users");
            }
        }

        public UserViewModel SelectedUser { get; set; }

        #endregion

        #region Filter List

        private string _name = string.Empty;

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
                this.OnPropertyChanged("Name");
            }
        }

        #endregion

        private Services.UsersService _serviceUsers;

        private ObservableCollection<UserViewModel> GetUsers()
        {
            try
            {
                var returnValue = new ObservableCollection<UserViewModel>();
                var users = this._serviceUsers.GetAll();
                //users = from Model.User u in users
                //        where u.UserName.ToUpperInvariant().Contains(_name.ToUpperInvariant())
                //        select u;
                users.ToList().ForEach(user => returnValue.Add(new UserViewModel(user)));
                return returnValue;
            }
            catch (Exception ex)
            {
                throw;
            }
            
        }

        public UserListNewViewModel()
        {
            _serviceUsers = appCtx.Instance.IoC.Resolve<Services.UsersService>();
        }

        #region Commands 
        public bool CanAddUser
        {
            get
            {
                return ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ?
                    ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).AddUsers : false;
            }
        }

        public bool CanDeleteUser
        {
            get
            {
                return ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ?
                    ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).DeleteUsers : false;
            }
        }

        public bool CanEditUser
        {
            get
            {
                return !(((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ?
                    ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).EditUsers : false);
            }
        }

        private ICommand showSaveCommand;

        public ICommand ShowSaveCommand
        {
            get
            {
                if (showSaveCommand == null)
                {
                    showSaveCommand = new Common.CommandBase(
                        i => this.SaveUser(),
                        i => ((UserViewModel)Properties.Settings.Default.AuthenticatedUser) != null ?
                            ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).EditUsers : false);
                }
                return showSaveCommand;
            }
        }

        private void SaveUser()
        {
            try
            {
                SelectedUser.ShowSaveCommand.Execute(null);
                this.OnPropertyChanged("Users");
                this.OnPropertyChanged("SelectedUser");
            }
            catch (Exception ex)
            { ModernDialog.ShowMessage(ex.Message, "Save User Failed", MessageBoxButton.OK); }
        }

        //for opening up the Delete User window
        private ICommand showDeleteCommand;

        public ICommand ShowDeleteCommand
        {
            get
            {
                if (showDeleteCommand == null)
                {
                    showDeleteCommand = new Common.CommandBase(
                        i => this.DeleteUser(),
                        i => this.CanDelete());
                }
                return showDeleteCommand;
            }
        }

        private void DeleteUser()
        {
            var result = ModernDialog.ShowMessage(string.Format("Do you want to delete user {0}?",
                SelectedUser.UserName), "Delete User Dialog", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                try
                {
                    SelectedUser.Delete();
                }
                catch (Exception ex)
                {
                    ModernDialog.ShowMessage(ex.Message, "Delete User Failed", MessageBoxButton.OK);
                }
            }
            this.OnPropertyChanged("Users");
            this.OnPropertyChanged("SelectedUser");

        }

        private bool CanDelete()
        {
            if (object.Equals((UserViewModel)Properties.Settings.Default.AuthenticatedUser, null)) return false;
            if (object.Equals(SelectedUser, null)) return false;
            return ((UserViewModel)Properties.Settings.Default.AuthenticatedUser).DeleteUsers;
        }

        private ICommand refreshCommand;

        public ICommand RefreshCommand
        {
            get
            {
                if (refreshCommand == null)
                {
                    refreshCommand = new Common.CommandBase(i => this.RefreshUsers(), null);
                }
                return refreshCommand;
            }
        }

        private void RefreshUsers()
        {
            //save user
            this.OnPropertyChanged("Users");
        }

        #endregion

    }
}
